/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.virtual.model.sax;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.xml.sax.SAXException;

/**
 * Implements a ContentSource based upon a Node.
 * 
 * @author Walter Luetgenau
 */
public class NodeContentSource extends AbstractContentSource implements UpdatableSAXContentSource {
    Node node;

    /** Creates a new instance of RepositoryDataContentSource
     * @param node The node, that will be transformed into SAX
     */
    public NodeContentSource(Session session, Node node) throws RepositoryException {
        super(session);
        this.node = node;
        setPath(node.getParent().getPath());
    }

    protected String getPath(Node node) throws RepositoryException {
        String nodeType = node.getPrimaryNodeType().getName();
        String parentPath = node.getParent().getPath();

        return parentPath + "/" + nodeType;
    }

    public void writeContent() throws RepositoryException, SAXException {
        if (handler == null) {
            return;
        }

        handler.startDocument();
        writeNode(node);
        terminatePath();
        handler.endDocument();
    }

    public String getQueryString() {
        return "";
    }

    public void setQueryString(String queryString) {
    }
}
